//
//  LastMovieViewController.m
//  LOL_PalmarHelper
//
//  Created by dlios on 15-6-24.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "LastMovieViewController.h"

@interface LastMovieViewController ()

@end

@implementation LastMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.ismyplay = 1;
    
    [self getData];
    
    self.array = [NSMutableArray array];

    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableV];
    
    self.tableV.delegate = self;
    
    self.tableV.dataSource = self;
    
    //   取消cell线
    
    self.tableV.separatorStyle = UITableViewCellEditingStyleNone;
    
    self.navigationItem.title = @"最新视频";
    
    
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"iconfont-fanhui" ofType:@"png"];
    UIImage *image1 = [UIImage imageWithContentsOfFile:path1];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消渲染
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image1 style:UIBarButtonItemStyleDone target:self action:@selector(buA)];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar1.png"] forBarMetrics:UIBarMetricsDefault];
    
    
    self.tableV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [_tableV release];
    
    
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    [self.view addSubview:self.hud];
    
    self.hud.labelText = @"正在加载...";
    
    [self.hud show:YES];
    
    [_hud release];
    
    
    
    
    
    
    
    
    
    
    
    
    

  
}

- (void)loadMoreData
{
    
    self.addPage ++;
    [self getData];
    
}





- (void)buA
{
    
  [self dismissViewControllerAnimated:YES completion:^{
      
  }];

   
    
}




- (void)dealloc
{
    [_data release];
    [_array release];
    [_tableV release];
    [_mycell release];
    [_VPlayer release];
    [_hud release];
    [_myvid release];
    [_myvideoController release];
    [super dealloc];
}



- (void)getData
{

    
    NSString *str = [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?src=duowan&action=l&sk=&pageUrl=&heroEnName=&tag=newest&p=%ld", self.addPage];

    
    [AFN AFNGetData:str andBlock:^(id respondes) {
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
                      
            NSArray *arr = respondes;
            
            for (NSDictionary *dic in arr) {
                
                VideoListModel *mdata = [[VideoListModel alloc] init];
                
                [mdata setValuesForKeysWithDictionary:dic];
                
                mdata.tag = @"newest";
                
                [self.array addObject:mdata];
                
             
                
                [mdata release];
                
            }
            
            [self.tableV reloadData];
            
            [self.hud hide:YES];
            
        }else if ([respondes isKindOfClass:[@"error" class]]) {
            
            
            
            [self.hud hide:YES];
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            

            
            
        }

        
        
        
        
        
    }];



}


// 点击cell触发方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    VideoListModel *mm = [self.array objectAtIndex:indexPath.row];
    
    self.myvid = mm.vid;
  
    [self playerGetData];
       
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *str = @"mycell";
    self.mycell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (self.mycell == nil) {
        self.mycell = [[VideoListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        
    }
    
    self.mycell.data = [self.array objectAtIndex:indexPath.row];
    
    
    return self.mycell;
}


// 设置cell高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100;
}




//  设置标题头

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return @"标题头";
//}
//




- (void)playerGetData
{
    
    
    NSString *str = [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=f&vid=%@&vu=null&sign=signxxxxx", self.myvid];
    
    
    [AFN AFNGetData:str andBlock:^(id respondes) {
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            NSDictionary *dic = respondes;
            
            NSDictionary *dic1 = [dic objectForKey:@"result"];
            
            if ([[dic1 objectForKey:@"items"] isKindOfClass:[NSDictionary class]]) {
                
                NSDictionary *dic2 = [dic1 objectForKey:@"items"];
                
                
                NSDictionary *dic3 = [dic2 objectForKey:@"350"];
                
                NSDictionary *dic4 = [dic3 objectForKey:@"transcode"];
                
                NSArray *arr = [dic4 objectForKey:@"urls"];
                
                NSString *str1 = [arr objectAtIndex:0];
                
                NSURL *url = [NSURL URLWithString:str1];
                
                self.myvideoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
                
                [self.myvideoController.moviePlayer prepareToPlay];
                
                [self presentMoviePlayerViewControllerAnimated:self.myvideoController];
                
                [self.myvideoController.moviePlayer play];
                
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buB1) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
                
                
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buB2) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
                
                
                
            }
            
            else
            {
                
                UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无此视频资源" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [self.view addSubview:al];
                [al show];
                [al release];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }
            
            
            
            
        }else if ([respondes isKindOfClass:[@"error" class]]) {
            
            
            
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
        
    }];
    
    
    
}


- (void)buB2
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
    
    if (self.ismyplay == 2) {
        
        [self.myvideoController.moviePlayer stop];
        
        [self.myvideoController.moviePlayer pause];
        
        self.ismyplay = 1;
        
        
        
    }
    
    if (self.ismyplay == 1) {
        
        self.ismyplay = 3;
        
    }
    
}

- (void)buB1
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    if (self.ismyplay == 1) {
        
        self.ismyplay = 2;
    }
    
    if (self.ismyplay == 3) {
        
        self.ismyplay = 1;
        
    }
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
